package com.class04.reference;

import com.class04.reference.data.Employee;
import org.junit.jupiter.api.Test;

import java.io.PrintStream;
import java.net.Socket;
import java.util.Comparator;
import java.util.function.*;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/29 9:02
 */
public class MethodRefTest {
    //    情况1：对象 :: 实例方法
//    消费型接口  Consumer  void accept(T o)
    @Test
    public void test1() {
//        1.匿名内部类
        Consumer<Integer> con1 = new Consumer<Integer>() {
            @Override
            public void accept(Integer o) {
                System.out.println(o);
            }
        };
        con1.accept(40);
//        2.lambda表达式
        Consumer<Integer> con2 = o -> System.out.println(o);
        con1.accept(40);
//        3.方法引用
        PrintStream out = System.out;
        Consumer<Integer> con3_1 = out::print;
//
        Consumer<Integer> con3 = System.out::println;

    }

    //供给型接口 Supplier<T>
    @Test
    public void test02() {
//        1.
        Employee employee = new Employee(100, "小马哥", 24, 100.1);
        Supplier<String> supplier = new Supplier<String>() {
            @Override
            public String get() {
                return employee.getName();
            }
        };
        System.out.println(supplier.get());
//        2.lambda表达式
        Supplier<String> supplier1 = () -> employee.getName();
//        3.方法引用
        Supplier<String> supplier3 = employee::getName;
        System.out.println(supplier3.get());

    }

    //    情况二：类::静态方法
    @Test
    public void test3() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        System.out.println(com.compare(12, 12));
//        2.lambda
        Comparator<Integer> com1 = (o1, o2) -> Integer.compare(o1, o2);
//        3.方法引用
        Comparator<Integer> com2 = Integer::compare;
    }

    @Test
    public void test4() {
        Function<Double, Long> function = new Function<Double, Long>() {
            @Override
            public Long apply(Double aDouble) {
//                round方法进行四舍五入
                return Math.round(aDouble);
            }
        };
        System.out.println(function.apply(99.1));
        //    2、lambda
        Function<Double, Long> function1 = aDouble ->
//                round方法进行四舍五入
                Math.round(aDouble);
        //    3、方法引用
        Function<Double, Long> function2 = Math::round;
    }

    @Test//情况三  类::实例方法(难)
    public void test5() {
//        1
        Comparator<String> com1 = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare("adc", "adb"));

//2.lambda
        Comparator<String> com2 = (o1, o2) ->
                o1.compareTo(o2);
//        3.方法引用
        Comparator<String> com3 = String::compareTo;

    }

    @Test
    public void test6() {
//        1.
        BiPredicate<String, String> biPredicate = new BiPredicate<String, String>() {
            @Override
            public boolean test(String s, String s2) {
                return s.equals(s2);
            }
        };
        boolean test = biPredicate.test("abc", "bcd");
        System.out.println("test = " + test);
//        2.lambda
        BiPredicate<String, String> biPredicate1 = (s, s2)
                -> s.equals(s2);
        System.out.println("=================================================");
        boolean test1 = biPredicate1.test("abc", "bcd");
        System.out.println("tet1 = " + test1);
//方法引用
        BiPredicate<String, String> biPredicate2 = String::equals;

    }

    public void test7() {
        Employee employee = new Employee(100, "小马哥", 24, 100.1);
        Function<Employee, String> function = new Function<Employee, String>() {
            @Override
            public String apply(Employee employee) {
                return employee.getName();
            }
        };
        System.out.println(function.apply(employee));
//        2.lambda表达式
        Function<Employee, String> function1 = employee1 ->
                employee.getName();
        System.out.println(function1.apply(employee));
//        3.
        Function<Employee, String> function2 = Employee::getName;
        System.out.println(function2.apply(employee));


    }


}

